package cn.itcast.reggie.controller;

import cn.itcast.reggie.common.BaseContext;
import cn.itcast.reggie.common.R;
import cn.itcast.reggie.pojo.ShoppingCart;
import cn.itcast.reggie.service.ShoppingCartService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.Date;
import java.util.List;
import java.util.Map;

@Slf4j
@RestController
@RequestMapping("/shoppingCart")
public class ShoppingCartController {
    @Autowired
    private ShoppingCartService shoppingCartService;


    @PostMapping("/add")
    public R add(@RequestBody ShoppingCart shoppingCart) {
        Long userId = BaseContext.getCurrentId();
        shoppingCart.setUserId(userId);
        //知道用户购买的是套餐|菜品
        Long dishId = shoppingCart.getDishId();
        Long setmealId = shoppingCart.getSetmealId();

        //判断用户之前是否购买过菜品|套餐
        QueryWrapper<ShoppingCart> queryWrapper = new QueryWrapper<>();
        if(dishId != null) {
            //用户购买的是菜品
            queryWrapper.eq("user_id",userId);
            queryWrapper.eq("dish_id",dishId);
        }else if(setmealId != null) {
            //用户购买的是套餐
            queryWrapper.eq("user_id",userId);
            queryWrapper.eq("setmeal_id",setmealId);
        }

        ShoppingCart sp = shoppingCartService.getOne(queryWrapper);
        if(sp == null) {
            //用户之前未购买过菜品|获取套餐
            shoppingCart.setCreateTime(LocalDateTime.now());
            shoppingCart.setNumber(1);
            shoppingCartService.save(shoppingCart);
        }else {
            //用户之前购买过，更新数量
            sp.setNumber(sp.getNumber() + 1);
            shoppingCartService.updateById(sp);
        }
        return R.success("加入购物车成功");
    }

    @GetMapping("/list")
    public R list(){
        Long userId = BaseContext.getCurrentId();
        QueryWrapper<ShoppingCart> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("user_id",userId);
        queryWrapper.orderByAsc("create_time");

        List<ShoppingCart> list = shoppingCartService.list(queryWrapper);

        return R.success(list);
    }

    @DeleteMapping("/clean")
    public R clean() {
        Long userId = BaseContext.getCurrentId();
        QueryWrapper<ShoppingCart> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("user_id",userId);
        shoppingCartService.remove(queryWrapper);
        return R.success("删除购物车数据成功");
    }

    @PostMapping("/sub")
    public R sub(@RequestBody Map<String,Long> map) {
        Long userId = BaseContext.getCurrentId();
        Long dishId = map.get("dishId");
        Long setmealId = map.get("setmealId");

        QueryWrapper<ShoppingCart> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("user_id",userId);
        if(dishId != null)  {
            queryWrapper.eq("dish_id",dishId);
        }else if(setmealId != null) {
            queryWrapper.eq("setmeal_id",setmealId);
        }
        ShoppingCart shoppingCart = shoppingCartService.getOne(queryWrapper);
        shoppingCart.setNumber(shoppingCart.getNumber() - 1);
        if(shoppingCart.getNumber() == 0) {
            shoppingCartService.removeById(shoppingCart.getId());
        }else {
            shoppingCartService.updateById(shoppingCart);
        }
        return R.success("购物项数量减少成功");
    }
 }   